<template>
	<div class="container add_edit">
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="上级模块">
				<el-select class="sele" v-model="form.parent_id" placeholder="请选择" clearable @change="selectChange">
					<el-option v-for="item in moduleOptions" :key="item._id" :label="item.name" :value="item._id">
					</el-option>
				</el-select>
				<div class="remark_txt">不选择，默认为上级</div>
			</el-form-item>
			<el-form-item label="模块名称" prop="name" ref="name">
				<el-input v-model="form.name" placeholder="请输入模块名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="模块图标">
				<el-tag type="info" class="icon" v-if="form.icon == ''" @click="iconDialog = true">请选择</el-tag>
				<el-tag type="info" closable class="icon pos_r" v-else @click="iconDialog = true" @close="emptyIcon">
					<span :class="[form.icon]"></span>
				</el-tag>
			</el-form-item>
			<el-form-item label="模块URL">
				<el-input v-model="form.url" placeholder="请输入模块URL" clearable></el-input>
				<div class="remark_txt">url前面加"/"</div>
			</el-form-item>
			<el-form-item label="模块排序">
				<el-input v-model.number="form.sort" type="number" :min="1" placeholder="请输入排序" clearable
					oninput="value = parseInt(value) || 1"></el-input>
			</el-form-item>
			<el-form-item label="是否启用">
				<el-switch v-model="form.enable" active-color="#ff6a6c" inactive-color="#bbb" :active-value="true"
					:inactive-value="false"></el-switch>
			</el-form-item>
			<el-form-item>
				<el-button :loading="formLoading" round icon="el-icon-check" class="confirm-btn"
					@click="submitForm('ruleForm')">提交</el-button>
				<el-button round icon="el-icon-back" class="return-btn" @click="returnPage">返回</el-button>
			</el-form-item>
		</el-form>
		<el-dialog class="icon_dialog" width="64%" title="图标" :visible.sync="iconDialog" :append-to-body="true"
			v-dialogDrag>
			<icon-page @getIcon="setIcon"></icon-page>
		</el-dialog>
	</div>
</template>

<script>
	import iconPage from './icon_page';
	const __name = 'opendb-admin-menus';
	export default {
		components: {
			iconPage
		},
		data() {
			return {
				dataId: '',
				formLoading: false,
				moduleOptions: [],
				curPid: '',
				form: {
					parent_id: '',
					name: '',
					icon: '',
					url: '',
					sort: 0,
					enable: true
				},
				iconDialog: false,
				rules: {
					name: [{
						required: true,
						message: '请输入模块名称',
						trigger: 'change'
					}]
				}
			};
		},
		methods: {
			async loadData() {
				await this.$db[__name]
					.noshopid()
					.noaction()
					.where({
						parent_id: ''
					})
					.tolist({
						page: 1,
						rows: 500,
						orderby: 'sort asc'
					})
					.then(res => {
						if (res.code == 200) {
							this.moduleOptions = res.datas.filter(x => !x.parent_id);
						}
					});

				if (!this.dataId) {
					this.getSort();
					return;
				}
				await this.$db[__name]
					.noshopid()
					.noaction()
					.tofirst(this.dataId).then(res => {
						if (res.code == 200) {
							for (let item in this.form) {
								this.form[item] = res.datas[item];
							}
							this.moduleOptions = this.moduleOptions.filter(x => x._id != res.datas._id);
						}
					});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						this.formLoading = true;
						if (!this.dataId) {
							this.$db[__name]
								.noshopid()
								.noaction()
								.add(this.form).then(res => {
									if (res.code == 200) {
										this.getOpenerEventChannel().emit('loadData');
										this.returnPage();
									}
									this.formLoading = false;
								}).catch(() => {
									this.formLoading = false;
								})
						} else {
							this.$db[__name]
								.noshopid()
								.noaction()
								.update(this.dataId, this.form).then(res => {
									if (res.code == 200) {
										this.getOpenerEventChannel().emit('loadData');
										this.returnPage();
									}
									this.formLoading = false;
								}).catch(() => {
									this.formLoading = false;
								})
						}
					}
				});
			},
			getSort() {
				this.$db[__name]
					.noshopid()
					.noaction()
					.where({
						parent_id: this.curPid
					})
					.getsort()
					.then(res => {
						if (res.code == 200) {
							this.form.sort = ++res.datas.sort;
						}
					});
			},
			selectChange(res) {
				this.curPid = res;
				this.getSort();
			},
			setIcon(data) {
				this.iconDialog = false;
				this.form.icon = data.type + ' ' + data.icon;
			},
			emptyIcon() {
				this.form.icon = '';
			},
			returnPage() {
				uni.$emit('closeTab');
				uni.redirectTo({
					url: '/pages/system_set/module/module'
				});
			}
		},
		created() {
			this.dataId = this.$route.query.id || '';
			this.loadData();
		}
	};
</script>

<style>
	.icon {
		cursor: pointer;
	}

	.icon::before {
		font-size: 18px;
	}

	.icon:hover {
		color: #ff6a6c;
	}

	.icon>>>.el-tag__close {
		position: absolute;
		right: -20px;
		top: 50%;
		transform: translateY(-50%);
	}

	.icon_dialog>>>.el-dialog__body {
		padding: 0 20px 20px 20px;
	}

	.icon_dialog>>>.el-tabs__content {
		height: 640px;
		overflow: overlay;
	}
</style>
